





import { Main } from "./Main";
import { MoveHexa } from "./MoveHexa";

const hoxa_list = [2,4,8,16,32,64,128,256,512,1024,2048];

export enum OnType{
    IDLE = "idle",
    ON = "onclicked",
    MOVE = "move"
}

const {ccclass, property} = cc._decorator;

@ccclass
export  class Hexagonal extends cc.Component {


    private _cirlceChange:cc.Node = null;

    /** 六边形类型 */
    private _Hexagonal_type:number = 0;

    /** 可移动六边形节点列表 */
    public MoveHexagonal_List:Array<cc.Node> = [];

    /** 保存可移动六边形的等级列表 */
    public Hexagonal_level:Array<number> = [];

    /** 可移动六边形的位置 */
    public MoveHexagonal_pos:Array<cc.Vec2> = [cc.v2(0,0),cc.v2(0,0),cc.v2(0,0),cc.v2(0,0),cc.v2(0,0),cc.v2(0,0),cc.v2(0,0)];

    public movefun:(touch)=>void;

    public endmovefun:(touch)=>void;

    public MoveNodePool:cc.NodePool = new cc.NodePool();

    /** 是否正在交换 */
    private _ischanging:boolean = false;

    /** 是否执行交换 */
    private _isperformexchange:boolean = false;

    /** 变化角度 */
    private _changeangle:number = 0;

    private hexaWidth:number = 0;
    private hexaleftleng:number = 0;
    private hexarightleng:number = 0;

    public getHexagonal_type(){
        return this._Hexagonal_type;
    }

    public setHexagonal_type(type:number){
        this._Hexagonal_type = type;
    }

    onLoad () {

        this.node.on(cc.Node.EventType.TOUCH_START,this.startOnclicked,this);
        this.node.on(cc.Node.EventType.MOUSE_UP,this.exchange,this);

        let wid = Main.Map.MapfragmentsWidth/2;
        this.hexaWidth = wid;
        let posx = wid*Math.sin(30*Math.PI /180);
        this.hexaleftleng = posx;
        let posy = wid*Math.cos(30*Math.PI /180);
        this.hexarightleng = posy;
        this._changeangle = 30;

        this.MoveHexagonal_pos[1] = cc.v2(-posx,posy);     //生成的方块位置
        this.MoveHexagonal_pos[2] = cc.v2(posx,-posy);
        this.MoveHexagonal_pos[3] = cc.v2(posx,posy);
        this.MoveHexagonal_pos[4] = cc.v2(-posx,-posy);
        this.MoveHexagonal_pos[5] = cc.v2(-wid,0);
        this.MoveHexagonal_pos[6] = cc.v2(wid,0);
        
        let spr = cc.loader.getRes("main/cirlceChange",cc.SpriteFrame);
        this._cirlceChange = new cc.Node();
        let movespr = this._cirlceChange.addComponent(cc.Sprite);
        movespr.spriteFrame = spr;
        movespr.sizeMode = 1;
        this.node.addChild(this._cirlceChange);
    }

    public startOnclicked (){
        cc.log("点击开始");
    }

    /** 交换两个元素 */
    public exchange (){

        cc.log("鼠标按下");
        if(!this._Hexagonal_type || Main.gameControl._onType != OnType.IDLE){
            cc.log("无法交换!");
            return;
        }
        this._ischanging = true;
        Main.gameControl._cantouch = false;
        if(this._Hexagonal_type <3){
            this._changeangle = 30;
        }else{
            this._changeangle = 90; 
        }

        let completecall = Main.CountCallback(this.MoveHexagonal_List.length,()=>{
            if(this._Hexagonal_type < 3){
                this._Hexagonal_type++;
            }else{
                this._Hexagonal_type = 1;
            }
            Main.gameControl._onType = OnType.IDLE;
            Main.gameControl._cantouch = true;
        });
        
        for(let i=0;i<this.MoveHexagonal_List.length;i++){

            let movehexa:MoveHexa = this.MoveHexagonal_List[i].getComponent(MoveHexa);
            movehexa.startChangepos(completecall);
        }
         
    }

    /** 移动留边形 */
    public MoveHexagonal (touch){
 
        let pos = this.node.convertTouchToNodeSpaceAR(touch);
        touch.target.setPosition(pos);
    }

    /** 离开六边形 */
    public LeaveHexagonal (touch){

    }

    /**
     * 添加一个可移动的六边形
     * @param Level 可生成的六边形等级
     * @param shape 形状  0 为单个  1为两个，上方元素在左上角   2为两个上方元素在右上角  3为横向
     * 
     */
    public addMoveHexagonal (Level:Array<number>,shape:number,moveOn:(touch)=>void,endOn:(touch)=>void){

        this.movefun = moveOn;
        this.endmovefun = endOn;
        this.Hexagonal_level = Level;
        this._Hexagonal_type = shape;
        cc.log(shape);
        switch (shape) {
            case 0:
                let level = Level[0];
                let pos = this.MoveHexagonal_pos[0];
                let name = 1;
                this.createHexagonal(level,pos,name.toString());
                break;
            case 1:
                for(let i=0;i<Level.length;i++){
                    let h_level = Level[i];
                    let pos = this.MoveHexagonal_pos[i+1];
                    let name = i+1;
                    this.createHexagonal(h_level,pos,name.toString());
                }
                
                break;
            case 2:
                for(let i=0;i<Level.length;i++){
                    let h_level = Level[i];
                    let pos = this.MoveHexagonal_pos[i+3];
                    let name = i+1;
                    this.createHexagonal(h_level,pos,name.toString());
                }
                break;
            case 3:
                for(let i=0;i<Level.length;i++){
                    let h_level = Level[i];
                    let pos = this.MoveHexagonal_pos[i+5];
                    let name = i+1;
                    this.createHexagonal(h_level,pos,name.toString());
                }
                break;
        
            default:
            cc.warn("传入类型错误");
                break;
        }
    
    }

    public addHexagonalToMap (index:number,pos:number,callback:Function){

        let movenode:cc.Node = this.MoveHexagonal_List[index];
        movenode.parent = Main.gameControl.mapNode;
        let moveHexa:MoveHexa = movenode.getComponent(MoveHexa);
        if(moveHexa){
            moveHexa.locationPos = pos;
        }
        movenode.setPosition(Main.gameControl._hexagonal_List[pos].position);   
        movenode.zIndex = 100;
        let level:Array<number> = this.Hexagonal_level;
        let data = Main.AllData.ConvertNumberingTodata(pos);
        Main.AllData.addMapData(level[index],data);
        //移除点击事件
        if(callback) callback();
        
    }

    /** 没有位置，回到原来位置 */
    public comeback (){
        if(this._Hexagonal_type == 0){
            this.MoveHexagonal_List[0].setPosition(this.MoveHexagonal_pos[0]);
        }else{
            let poindex = 0;
            if(this._Hexagonal_type ==1){
              poindex = 1;
            }else if(this._Hexagonal_type == 2){
              poindex = 3;
            }else{
                poindex = 5;
            }
            for(let i=0;i<this.MoveHexagonal_List.length;i++){
                let movenode:cc.Node = this.MoveHexagonal_List[i];
                movenode.setPosition(this.MoveHexagonal_pos[i+poindex]);
            }
        }
    }

    /** 计算相对距离 */
    public CalculateDistance (posIndex:number,type:number){

        let relative:cc.Vec2 = cc.v2(0,0);
        let wid = Main.Map.MapfragmentsWidth;
        let posx = wid*Math.sin(30*Math.PI /180);
        let posy = wid*Math.cos(30*Math.PI /180);
        if(type == 1){

            if(posIndex > 1){
                relative = cc.v2(posx,-posy);
                return relative;
            }else{
                relative = cc.v2(-posx,posy);
                return relative;
            }
        }else if(type == 2){
            if(posIndex > 1){
                relative = cc.v2(-posx,-posy);
                return relative;
            }else{
                relative = cc.v2(posx,posy);
                return relative;
            }
        }else{
            if(posIndex > 1){
                relative = cc.v2(wid,0);
                return relative;
            }else{
                relative = cc.v2(-wid,0);
                return relative;
            }
        }
    }

    private createHexagonal (Level:number,pos:cc.Vec2,name:string){

        let hexa_spr = cc.loader.getRes('main/' + Level,cc.SpriteFrame);
        let movenode = null;
        let movespr = null;
        if(this.MoveNodePool.size() > 0){
            movenode = this.MoveNodePool.get();
            movenode.name = name;
            movespr = movenode.getComponent(cc.Sprite);
        }else{
            movenode = new cc.Node(name);
            movespr = movenode.addComponent(cc.Sprite);
            movenode.addComponent(MoveHexa);
        }
        let moveHexa = movenode.getComponent(MoveHexa);
        moveHexa.Level = Level;
        movespr.spriteFrame = hexa_spr;
        movespr.sizeMode = 1;
        this.node.addChild(movenode);
        movenode.setPosition(pos);
        moveHexa.initview();
        movenode.on(cc.Node.EventType.TOUCH_MOVE,this.movefun,Main.gameControl);
        movenode.on(cc.Node.EventType.TOUCH_END,this.endmovefun,Main.gameControl);
        movenode.on(cc.Node.EventType.TOUCH_CANCEL,this.endmovefun,Main.gameControl);
        this.MoveHexagonal_List.push(movenode);
        
    }

    public Hexagonal_lv(level:number,node:cc.Node){
        if(!level){
            cc.warn("等级错误");
        }
        let hexa_spr = cc.loader.getRes('main/' + level,cc.SpriteFrame);
        let movespr = node.getComponent(cc.Sprite);
        let moveHexa = node.getComponent(MoveHexa);
        moveHexa.Level = level;
        movespr.spriteFrame = hexa_spr;
    }

    /** 移除点击事件 清除数据*/
    public removeHexagonalOn (){

        for(let i=0;i<this.MoveHexagonal_List.length;i++){
            let movenode = this.MoveHexagonal_List[i];
            movenode.off(cc.Node.EventType.TOUCH_MOVE,this.movefun,Main.gameControl);
            movenode.off(cc.Node.EventType.TOUCH_END,this.endmovefun,Main.gameControl);
            movenode.off(cc.Node.EventType.TOUCH_CANCEL,this.endmovefun,Main.gameControl);
        }
        this.MoveHexagonal_List = [];
    }

    private addsingleHexagonal(){

    }

    start () {

    }

    update (dt) {


        if(this._isperformexchange){

            this._changeangle += dt*100;
            if(this._Hexagonal_type <3){
                if(this._changeangle >= 90){                    
                    cc.log("到达位置==>");
                    this._changeangle = 90;
                    this._isperformexchange = false;
                    this._ischanging = false;
                    if(this._Hexagonal_type < 3){
                        this._Hexagonal_type++;
                    }else{
                        this._Hexagonal_type = 1;
                    }
                }
            }else{
                if(this._changeangle >= 150){                    
                    cc.log("到达位置==>");
                    this._changeangle = 150;
                    this._isperformexchange = false;
                    this._ischanging = false;
                    if(this._Hexagonal_type < 3){
                        this._Hexagonal_type++;
                    }else{
                        this._Hexagonal_type = 1;
                    }
                }
            }
            
            let posx = this.hexaWidth*Math.sin(this._changeangle*Math.PI /180);
            let posy = this.hexaWidth*Math.cos(this._changeangle*Math.PI /180);
            for(let i=0;i<this.MoveHexagonal_List.length;i++){
                let hexaNode = this.MoveHexagonal_List[i];
                if(i==0){
                    hexaNode.setPosition(-posx,posy);
                }else{
                    hexaNode.setPosition(posx,-posy);
                }
            }
            
        }
    }
}